package com.example.composedemo

import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview
import androidx.lifecycle.ViewModel
import com.example.composedemo.ui.theme.ComposeDemoTheme
import com.example.composedemo.widget.CompositionTest
import com.example.composedemo.widget.WelcomePage
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.async
import kotlinx.coroutines.cancel
import kotlinx.coroutines.delay
import kotlinx.coroutines.job
import kotlinx.coroutines.launch
import kotlinx.coroutines.supervisorScope
import kotlin.coroutines.coroutineContext


class MainActivity : ComponentActivity() {

    private val vm by viewModels<TestViewModel>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            ComposeDemoTheme {
                // // A surface container using the 'background' color from the theme
                // Surface(modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background) {
                //     Greeting("Android")
                // }

                CompositionTest()
            }
        }
    }
}

class TestViewModel : ViewModel() {

    private val scope = CoroutineScope(SupervisorJob())

    fun testScope() {
        scope.launch {
            Log.d("luoyubo", "testScope: 1")
            delay(2000)
            1 / 0
        }
        scope.launch {
            delay(1000)
            Log.d("luoyubo", "testScope: 2")
        }
    }


    fun testScope2() {
        scope.launch {
            supervisorScope {
                val async1 = async {
                    Log.d("luoyubo", "async1")
                    delay(1000)
                    1 / 0
                }

                async {
                    delay(1000)
                    Log.d("luoyubo", "async2")
                }

                async1.await()
            }
        }
    }

    fun testJob() {
        Log.d("luoyubo", "testJob: ${scope.coroutineContext.job.isActive}")
        scope.launch {
            delay(1000)
        }
        Log.d("luoyubo", "testJob: ${scope.coroutineContext.job.isCompleted}")

        scope.cancel()
    }

    suspend fun scopeCheck(scope: CoroutineScope) {
        println(scope.coroutineContext === coroutineContext)
    }
}

@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
    val context = LocalContext.current
    Box(Modifier.fillMaxSize()) {

        Column(modifier = modifier.fillMaxSize()) {
            Button(onClick = {
                // try {
                1 / 0
                // } catch (e: Exception) {
                //     e.printStackTrace()
                //     Log.e("test", "Exception test")
                // }
            }) {
                Text(text = "测试一下崩溃")
            }
            Button(onClick = {
                Toast.makeText(context, "haha", Toast.LENGTH_SHORT).show()
            }, Modifier.align(Alignment.CenterHorizontally)) {
                Text(text = "hhhh")
            }
        }


    }
}

@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
    ComposeDemoTheme {
        WelcomePage()
        // Greeting("Android")
    }
}